package com.css.zfzx.fxpg.modules.percent.damage.repository.impl;

import com.css.zfzx.fxpg.modules.percent.damage.repository.FxDamagePercentNativeRepository;
import com.css.zfzx.fxpg.modules.percent.damage.repository.entity.FxDamagePercentEntity;
import com.css.zfzx.fxpg.modules.percent.damage.viewobjects.FxDamagePercentQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-13
 */
@Repository
public class FxDamagePercentNativeRepositoryImpl implements FxDamagePercentNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxDamagePercentNativeRepositoryImpl.class);


    @Override
    public Page<FxDamagePercentEntity> queryFxDamagePercents(FxDamagePercentQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_damage_percent ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            whereSql.append(" and structure_type = :structureType");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxDamagePercentEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_damage_percent ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            query.setParameter("structureType", queryParams.getStructureType());
            countQuery.setParameter("structureType", queryParams.getStructureType());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxDamagePercentEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxDamagePercentEntity> fxDamagePercentPage = new PageImpl<>(list, pageable, count.longValue());
        return fxDamagePercentPage;
    }

    @Override
    public List<FxDamagePercentEntity> listFxDamagePercents(FxDamagePercentQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_damage_percent ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            whereSql.append(" and structure_type = :structureType");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxDamagePercentEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            query.setParameter("structureType", queryParams.getStructureType());
        }
        List<FxDamagePercentEntity> list = query.getResultList();
        return list;
    }
}
